// components/file-summary/file-summary.ts
import common from "../../common/index";
import {createDialogueId, uuid} from "../../utils/util";
import {ChatItemType, ChatStatusEnum} from "../../types/index";
import api from "../../api/index";

const app = getApp()
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    fileInfo: {
      type: Object,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    navBarHeight: app.globalData.navBarHeight,
    uid: '',
    file: null,
    offHeight: '',
    chatItem: {}
  },

  lifetimes: {
    attached() {
      const user = common.getAppUserInfo()
      this.data.uid = user.uid
      this.setData({
        file: this.properties.fileInfo
      }, () => {
        wx.nextTick(() => {
          const query = this.createSelectorQuery();
          query.select('#file_info_box').boundingClientRect((res) => {
            const offHeight = res.height + this.data.navBarHeight + this.data.statusBarHeight + 20
            this.setData({
              offHeight
            })
            this.getFileMindmap()
          }).exec();
        })
      })
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    closePage() {
      this.triggerEvent('close')
    },
    executeChat(params: any){
      this.data.chatItem = common.getDefChatItem()
      this.data.chatItem.answerKey = 'mindmap'
      api.chat('/aispire/file_read/generate_stream_mindmap', params, this.data.chatItem, {
        answerCall: () => {
          wx.hideLoading()
          this.data.chatItem.aispireObj = app.towxml(this.data.chatItem.aispire, 'markdown', {
            theme: 'answer'
          });
          this.setData({
            chatItem: this.data.chatItem
          })
        },
        completeCall: () => {
          this.setData({
            chatItem: this.data.chatItem
          })
          wx.hideLoading()
        }
      })
    },
    async getFileMindmap() {
      wx.showLoading({
        title: '加载中',
        mask: true,
      })
      const uid = this.data.uid
      const params = {
        uid,
        file_id: this.data.file.id
      }
      const errorMsg = await common.getCheckMsg(params, true)
      if (errorMsg) {
        wx.hideLoading()
        wx.showToast({
          title: errorMsg
        })
        return
      }
      this.executeChat(params)
    }
  }
})